﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Threading.Tasks;

namespace DigDes.DSchool.SUPS.DataAccess.Database
{
    public class ReportDAC : BaseDAC
    {
        public static IEnumerable<DigDes.DSchool.SUPS.DataAccess.Interface.ReportModelDTO> GetReport()
        {
            using (var sqlConn = new SqlConnection(connectionString))
            using (var sqlCmd = sqlConn.CreateCommand())
            {
                sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                sqlCmd.CommandText = "GetReport";
                sqlConn.Open();

                using (SqlDataReader reader = sqlCmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        DigDes.DSchool.SUPS.DataAccess.Interface.ReportModelDTO row = new DigDes.DSchool.SUPS.DataAccess.Interface.ReportModelDTO();

                        row.InventoryNumber = ConvertFromDBDataToString(reader["InventoryNumber"]);
                        row.TimeOnWay = ConvertFromDBDataToString(reader["TimeOnWay"]);
                        row.TimeOnLoad = ConvertFromDBDataToString(reader["TimeOnLoad"]);
                        row.TimeOnUnload = ConvertFromDBDataToString(reader["TimeOnUnload"]);

                        yield return row;
                    }
                }
            }
        }

        public static IEnumerable<DigDes.DSchool.SUPS.DataAccess.Interface.ReportModelDTO> GetReportByNumber(string number)
        {
            using (var sqlConn = new SqlConnection(connectionString))
            using (var sqlCmd = sqlConn.CreateCommand())
            {
                sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                sqlCmd.CommandText = "GetReportByNumber";
                sqlCmd.Parameters.Add(new SqlParameter("@number", number));
                sqlConn.Open();

                using (SqlDataReader reader = sqlCmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        DigDes.DSchool.SUPS.DataAccess.Interface.ReportModelDTO row = new DigDes.DSchool.SUPS.DataAccess.Interface.ReportModelDTO();

                        row.InventoryNumber = ConvertFromDBDataToString(reader["CoachNumber"]);
                        row.TimeOnWay = ConvertFromDBDataToString(reader["TimeOnWay"]);
                        row.TimeOnLoad = ConvertFromDBDataToString(reader["TimeOnLoad"]);
                        row.TimeOnUnload = ConvertFromDBDataToString(reader["TimeOnUnload"]);

                        yield return row;
                    }
                }
            }
        }
    }
}
